package com.dk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dk.model.Report;
import com.dk.mapper.ReportMapper;
import com.dk.model.Scheduld;
import com.dk.model.User;
import com.dk.service.ReportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dk.service.ScheduldService;
import com.dk.service.UserService;
import com.dk.service.WorkstationsService;
import com.dk.vo.EchartsVO;
import com.dk.vo.ReportVO;
import com.dk.vo.UserVO;
import com.dk.vo.WorkstationsVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wbh
 * @since 2023-03-22
 */
@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements ReportService {

    @Resource
    private ReportMapper reportMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private ScheduldService scheduldService;

    @Override
    public List<ReportVO> getInfoList(ReportVO reportVO) {
        /*报工类型,工作站名称,生产工单编号,产品物料编码,产品物料名称,报工人,记录人,状态*/
        LambdaQueryWrapper<Report> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(reportVO.getReportType()), Report::getReportType, reportVO.getReportType());//类型
        wrapper.like(StringUtils.isNotBlank(reportVO.getMaterialId()), Report::getMaterialId, reportVO.getMaterialId());//产品物料编码
        wrapper.like(StringUtils.isNotBlank(reportVO.getMaterialName()), Report::getMaterialName, reportVO.getMaterialName());//产品物料名称
        wrapper.like(StringUtils.isNotBlank(reportVO.getWorkId()), Report::getWorkId, reportVO.getWorkId());//生产工单编号
        wrapper.eq(StringUtils.isNotBlank(reportVO.getReportState()), Report::getReportState, reportVO.getReportState());//状态
        List<Report> reportList = reportMapper.selectList(wrapper);
        //去查所有的人员
        List<UserVO> userVOList = userService.getInfoList(new UserVO());
        List<ReportVO> reportVOList = reportList.stream().map(report -> {
            ReportVO reportVO1 = new ReportVO();
            //todo 需要优化
            userVOList.stream().forEach(user -> {
                if (report.getCheckPersonid() == user.getUserId()) {
                    reportVO1.setCheckPersonName(user.getUserName());
                }
                if (report.getReportPersonid() == user.getUserId()) {
                    reportVO1.setReportPersonName(user.getUserName());
                }
            });
            /*for (UserVO user : userVOList) {
                if (report.getCheckPersonid() == user.getUserId()) {
                    reportVO1.setCheckPersonName(user.getUserName());
                }
                if (report.getReportPersonid() == user.getUserId()) {
                    reportVO1.setReportPersonName(user.getUserName());
                }
            }*/
            reportVO1.setWorkstationName(scheduldService.getWorkstationNameByScheduldById(report.getScheduldId()));
            BeanUtils.copyProperties(report, reportVO1);
            return reportVO1;
        }).collect(Collectors.toList());
        return reportVOList;
    }

    @Override
    public void insertInfoReport(ReportVO reportVO) {
        Report report = new Report();
        BeanUtils.copyProperties(reportVO, report);
        reportMapper.insert(report);
    }

    @Override
    public ReportVO getInfoReportById(Integer id) {
        Report report = reportMapper.selectById(id);
        ReportVO reportVO = new ReportVO();
        BeanUtils.copyProperties(report, reportVO);
        return reportVO;
    }

    @Override
    public void updateInfoReportById(ReportVO reportVO) {
        Report report = new Report();
        BeanUtils.copyProperties(reportVO, report);
        reportMapper.updateById(report);
    }

    @Override
    public void deleteInfoReportById(Integer id) {
        reportMapper.deleteById(id);
    }

    @Override
    public List<EchartsVO> getInfoToEchers(Integer flag) {
        List<EchartsVO> echartsList = new ArrayList<>();
        List<Report> reportList = reportMapper.getSumByReportNumGroupByWordstationId();
        Report report = reportMapper.getSumByReportNum();
        if (flag == 1){
            for (Report report1 : reportList) {
                EchartsVO vo  =new EchartsVO();
                vo.setName(report1.getWorkstationName());
                vo.setValue(getPercentStr(report1.getCount(),report.getCount()));
                echartsList.add(vo);
            }
        }else{
            for (Report report1 : reportList) {
                EchartsVO vo  =new EchartsVO();
                vo.setName(report1.getWorkstationName());
                String percentStr = getPercentStr(report1.getSuccessNum(), report1.getCount());
                vo.setSuccess(percentStr);
                vo.setFail(String.valueOf(100 - Integer.valueOf(percentStr)));
                echartsList.add(vo);
            }
        }
        return echartsList;
    }

    /**
     * 得到百分比的字符串，比如传参 1,3，返回33%
     */
    public String getPercentStr(Integer diff, Integer sum){
        DecimalFormat df = new DecimalFormat("0");//格式化小数
        float num = (float)diff / sum * 100;
        String str = df.format(num);
        return str ;
    }
}
